/**
 * @名字: 打开全文处理事件
 * @详情: 统一进行处理, id和fulltextSelect传一个参数即可
 * @参数: {id: 全文id, fulltextRow: 全文选择项, fulltextTableData: 全文表格数据 }
 * @返回值:
 */
import http from "@/api/index";
import { preType, imgType, videoType, audioType, ofdType } from "./contentType";
interface IFulltext {
  id: number | string;
  fulltextRow: any;
  fulltextTableData: any;
  IsAllow: boolean;
  config: {
    visible: boolean;
    fileList: any[];
    type: string;
    download: boolean;
    print: boolean;
  };
}
export const previewFulltext = ({ id, fulltextRow, fulltextTableData, IsAllow, config }: IFulltext, proxy: any) => {
  config.fileList = [];
  const AI_ID = id || fulltextRow.AI_ID;
  http.get("Master_Base_AllInfos/GetById", { objId: AI_ID }).then((res: any) => {
    if (res.Model) {
      const tempObj = res.Model;
      if (!IsAllow) {
        proxy.$message.warning("禁止打开全文!");
        return;
      }
      const type = preType[tempObj.AI_Source_Extension.toLowerCase()];
      console.log("预览全文", type);
      if (type) {
        // 图片类或者视频类
        if (imgType.includes(type)) {
          const filterData = fulltextTableData.filter(
            (v: { AI_Source_Extension: string }) =>
              v.AI_Source_Extension && imgType.includes(preType[v.AI_Source_Extension.toLowerCase()])
          );
          console.log("过滤所有图片", filterData);
          http.post("MasterBaseAllInfos/AcquireFilesPath", { aiids: filterData.map((v: any) => v.AI_ID) }).then((res2: any) => {
            config.visible = true;
            config.type = type;
            config.fileList.push(...res2.List);
          });
        } else if (videoType.includes(type)) {
          http.get("MasterBaseAllInfos/AcquireFilePath", { AI_ID }).then((res2: any) => {
            config.visible = true;
            config.type = type;
            config.fileList.push(res2.Model);
          });
        } else if (audioType.includes(type)) {
          http.get("MasterBaseAllInfos/AcquireFilePath", { AI_ID }).then((res2: any) => {
            config.visible = true;
            config.type = type;
            config.fileList.push(res2.Model);
          });
        } else if (ofdType.includes(type)) {
          http.get("MasterBaseAllInfos/AcquireFilePath", { AI_ID }).then((res2: any) => {
            config.visible = true;
            config.type = type;
            config.fileList.push(res2.Model);
          });
        } else {
          // 主要是pdf类
          http.get("MasterBaseAllInfos/AcquireFilePath", { AI_ID }).then((fileRes: any) => {
            if (fileRes.Mark) {
              config.visible = true;
              config.type = type;
              // 下面的.toLowerCase().replace('.doc', '.pdf').replace('.docx', '.pdf')转换是因为打的是PDf，显示的是WORD
              config.fileList = [
                {
                  name: fileRes.Model.FileName.replace(".docx", ".pdf").replace(".doc", ".pdf"),
                  src: fileRes.Model.Path
                }
              ];
              config.download = IsAllow || !tempObj.IsBanOnDownload;
              config.print = IsAllow || !tempObj.IsBanOnPrint;
            } else {
              proxy.$warning({
                title: "提示",
                content: fileRes.Msg
              });
            }
          });
        }
      } else {
        proxy.$message.warning("此类型文件不支持在线预览");
      }
    } else {
      proxy.$message.warning("此文件不存在");
    }
  });
};
